#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

#include <QPainterPath>
#include <QRegion>
#include <QTimer>
#include <QLineEdit>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->centralwidget->setMouseTracking(true);
    ui->verticalLayout->setContentsMargins(0, 0, 0, 0);

    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
//    setWindowOpacity(0.9);

    init();

//    bigBtn = new BigBtn(QSize(200, 200), 10, this);
//    bigBtn->setText("Test");
//    bigBtn->move(QPoint(this->width()/2, this->height()/2) -
//                    QPoint(bigBtn->width()/2, bigBtn->height()/2));
//    bigBtn->show();

//    smallBtn = new SmallBtn(QSize(100, 40), ui->mainDisplayWidget);
//    smallBtn->setText("Test");
//    smallBtn->move(QPoint(ui->mainWidget->width()/2, ui->mainWidget->height()/2) -
//                    QPoint(smallBtn->width()/2, smallBtn->height()/2));
//    smallBtn->show();

    QWidget *displayWidget = new QWidget(ui->mainWidget);
    QVBoxLayout *displayLayout = new QVBoxLayout(displayWidget);
    displayWidget->setLayout(displayLayout);


    selectionItem = new SelectionItem("Test", "this a test", ui->mainWidget);
    selectionItem2 = new SelectionItem("Test2", "This is a tes2.", ui->mainWidget);
    bigBtn = new BigBtn(QSize(100, 100), 10, ui->mainWidget);
    std::cout << bigBtn->sizeHint().width() << " " << bigBtn->sizeHint().height() << std::endl;
    bigBtn->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

    displayLayout->addWidget(selectionItem);
    displayLayout->addWidget(selectionItem2);
    displayLayout->addWidget(bigBtn);

    ui->displayLaout->addWidget(displayWidget);

//    QLineEdit *lineEdit = new QLineEdit(this);
//    QPushButton *testButton = new QPushButton(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init() {
    QPainterPath path;
    ui->mainWidget->resize(900, 800);
    path.addRect(ui->mainWidget->rect());

    QRegion mask(path.toFillPolygon().toPolygon());
    ui->mainWidget->setMask(mask);

    ui->mainWidget->setObjectName("mainWidget");
    QColor mainBackGround = QColor(251, 251, 251);
    QString mainStyle;  //设置样式
    mainStyle = "QWidget#mainWidget{background-color:" +
            mainBackGround.name() +
            QString::asprintf(";border-radius:%dpx", 10) + "}";
    ui->mainWidget->setStyleSheet(mainStyle);
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::MouseButton::NoButton) {
        mousePressed = false;
    }
    if(!mousePressed){
        mouseState = 0;
        if(!maximized && abs(event->pos().x() - ui->mainWidget->pos().x()) < 5)
            mouseState |= AT_LEFT;
        if(!maximized && abs(event->pos().y() - ui->mainWidget->pos().y()) < 5)
            mouseState |= AT_TOP;
        if(!maximized && abs(event->pos().x() - ui->mainWidget->pos().x() - ui->mainWidget->width()) < 5)
            mouseState |= AT_RIGHT;
        if(!maximized && abs(event->pos().y() - ui->mainWidget->pos().y() - ui->mainWidget->height()) < 5)
            mouseState |= AT_BOTTOM;

        if(mouseState == AT_TOP_LEFT  || mouseState == AT_BOTTOM_RIGHT)
            setCursor(Qt::SizeFDiagCursor);
        else if(mouseState == AT_TOP_RIGHT || mouseState == AT_BOTTOM_LEFT)
            setCursor(Qt::SizeBDiagCursor);
        else if(mouseState & (AT_LEFT | AT_RIGHT))
            setCursor(Qt::SizeHorCursor);
        else if(mouseState & (AT_TOP | AT_BOTTOM))
            setCursor(Qt::SizeVerCursor);
        else
            unsetCursor();
    }
    else{
        if(mouseState == 0){
            if(maximized){
                qreal wRatio = (double)event->pos().x() / (double)ui->mainWidget->width();
                controlWindowScale();
                this->move(QPoint(event->globalPos().x() - ui->mainWidget->width() * wRatio, -30));
                pressedPos = QPoint(ui->mainWidget->width() * wRatio, event->pos().y());
            }
            else
                this->move(event->globalPos() - pressedPos);
        }
        else{
            QPoint d = event->globalPos() - frameGeometry().topLeft() - pressedPos;
            if(mouseState & AT_LEFT){
                this->move(this->frameGeometry().x() + d.x(), this->frameGeometry().y());
                this->resize(this->width() - d.x(), this->height());
            }
            if(mouseState & AT_RIGHT){
                this->resize(this->width() + d.x(), this->height());
            }
            if(mouseState & AT_TOP){
                this->move(this->frameGeometry().x(), this->frameGeometry().y() + d.y());
                this->resize(this->width(), this->height() - d.y());
            }
            if(mouseState & AT_BOTTOM){
                this->resize(this->width(), this->height() + d.y());
            }
        }
        pressedPos = event->globalPos() - this->frameGeometry().topLeft();
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::MouseButton::LeftButton) {
        mousePressed = true;
        pressedPos = event->globalPos() - this->frameGeometry().topLeft();
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    mousePressed = false;
}

void MainWindow::resizeEvent(QResizeEvent* event) {
    QPainterPath path;
    path.addRect(ui->mainWidget->rect());
    QRegion mask(path.toFillPolygon().toPolygon());
    ui->mainWidget->setMask(mask);
}

void MainWindow::controlWindowScale()
{

}

